package org.itfarmer.service.impl;

import java.util.Date;
import java.util.List;

import org.itfarmer.dao.ComplaintDAO;
import org.itfarmer.dao.pojo.Complaint;
import org.itfarmer.service.ComplaintService;

public class ComplaintServiceImpl implements ComplaintService {

	private ComplaintDAO complaintDAO;
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.BaseService#countService()
	 */
	public Integer countService() {
		Integer rows = complaintDAO.count(Complaint.class);
		return rows;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#searchALL(java.lang.Integer, java.lang.Integer)
	 */
	public List<Complaint> searchALL(Integer pageNow, Integer pageSize) {
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List<Complaint> list = complaintDAO.read(Complaint.class, firstRow, maxRow);
		return list;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#searchByCid(java.lang.Integer)
	 */
	public Complaint searchByCid(Integer cid) {
		if (cid == null) {
			return null;
		}
		Complaint complaint = complaintDAO.read(Complaint.class, cid);
		return complaint;
	}

	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#searchByWorkid(java.lang.Integer)
	 */
	public List<Complaint> searchByWorkid(Integer workid, Integer pageNow, Integer pageSize) {
		if (workid == null) {
			return null;
		}
		Integer firstRow = (pageNow - 1) * pageSize;
		Integer maxRow = pageSize;
		List list = complaintDAO.readByWorkid(workid, firstRow, maxRow);
		return list;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#insert(org.itfarmer.dao.pojo.Complaint)
	 */
	public boolean insert(Complaint complaint) {
		if (complaint == null) {
			return false;
		}
		Integer pk = complaintDAO.create(complaint);
		return pk == null ? false : true;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#replyComplaint(java.lang.Integer, java.lang.String)
	 */
	public boolean replyComplaint(Integer cid, String replyContent) {
		if (cid == null) {
			return false;
		}
		Complaint complaint = complaintDAO.read(Complaint.class, cid);
		complaint.setReply(replyContent);
		complaint.setReplydate(new Date());
		boolean checked = complaintDAO.update(complaint);
		return checked;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.itfarmer.service.ComplaintService#makeRead(java.lang.Integer)
	 */
	public boolean makeRead(Integer cid) {
		if (cid == null) {
			return false;
		}
		Complaint complaint = complaintDAO.read(Complaint.class, cid);
		complaint.setIsread(1);
		boolean checked = complaintDAO.update(complaint);
		return checked;
	}

	public ComplaintDAO getComplaintDAO() {
		return complaintDAO;
	}

	public void setComplaintDAO(ComplaintDAO complaintDAO) {
		this.complaintDAO = complaintDAO;
	}

	
}
 